package edu.odu.cs.sstore.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.odu.cs.sstore.domain.Authority;
import edu.odu.cs.sstore.domain.Course;
import edu.odu.cs.sstore.domain.Document;
import edu.odu.cs.sstore.domain.User;
import edu.odu.cs.sstore.repository.AuthorityRepository;
import edu.odu.cs.sstore.repository.CourseRepository;
import edu.odu.cs.sstore.repository.DocumentRepository;
import edu.odu.cs.sstore.repository.UserRepository;

@Service
@Scope(value = "singleton")
public class Bootstrap {

	private boolean completed = false;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private CourseRepository courseRepository;
	
	@Autowired
	private DocumentRepository docRepository;
	
	@Autowired
	private AuthorityRepository authorityRepository;

	@Transactional
	public void execute() {
		
		if(completed) return;
		
		// Create Authorities (Roles)
		
		Authority ROLE_ADMIN = authorityRepository.create("ROLE_ADMIN");
	    Authority ROLE_FACULTY = authorityRepository.create("ROLE_FACULTY");
	    
	    // Create Users
	    
	    // Administrator 
	    User adminUser = new User();
	    adminUser.setUsername("admin");
	    adminUser.setName("Administrator");
	    adminUser.setPassword("password");
	    adminUser.addAuthority(ROLE_ADMIN);
	    adminUser = userRepository.saveOrUpdate(adminUser);
	    
	    // Faculty #1
	    User facultyUser1 = new User();
	    facultyUser1.setUsername("234567");
	    facultyUser1.setName("Dr. Mukka");
	    facultyUser1.setPassword("password");
	    facultyUser1.addAuthority(ROLE_FACULTY);
	    facultyUser1 = userRepository.saveOrUpdate(facultyUser1);
	    
	    // Faculty #2
	    User facultyUser2 = new User();
	    facultyUser2.setUsername("345678");
	    facultyUser2.setName("Dr. Wahab");
	    facultyUser2.setPassword("password");
	    facultyUser2.addAuthority(ROLE_FACULTY);
	    facultyUser2 = userRepository.saveOrUpdate(facultyUser2);
		

	    // Student #1
	    User studentUser1 = new User();
	    studentUser1.setUsername("485447");
	    studentUser1.setName("Jared Shields");
	    studentUser1.setPassword("password");
	    studentUser1 = userRepository.saveOrUpdate(studentUser1);
	    
	    // Student #2
	    User studentUser2 = new User();
	    studentUser2.setUsername("78642");
	    studentUser2.setName("Elizabeth Johnson");
	    studentUser2.setPassword("password");
	    studentUser2 = userRepository.saveOrUpdate(studentUser2);
	    
	    // Student #3
	    User studentUser3 = new User();
	    studentUser3.setUsername("64895");
	    studentUser3.setName("Peter Pan");
	    studentUser3.setPassword("password");
	    studentUser3 = userRepository.saveOrUpdate(studentUser3);
	    
	    // Student #4
	    User studentUser4 = new User();
	    studentUser4.setUsername("34897");
	    studentUser4.setName("Mickey Mouse");
	    studentUser4.setPassword("password");
	    studentUser4 = userRepository.saveOrUpdate(studentUser4);

	    // Courses
	    Course course1 = new Course();
	    course1.setCrn(25675L);
	    course1.setFaculty(facultyUser1);
	    course1.setCode("CS772/882");
	    course1.setName("Network and Application Security");
	    course1 = courseRepository.saveOrUpdate(course1);
	    	    
	    Course course2 = new Course();
	    course2.setCrn(56782L);
	    course2.setFaculty(facultyUser1);
	    course2.setCode("CS471");
	    course2.setName("Operating System Concepts");
	    course2.addStudent(studentUser3);
	    course2.addStudent(studentUser4);
	    course2 = courseRepository.saveOrUpdate(course2);
	    
	    Course course3 = new Course();
	    course3.setCrn(57842L);
	    course3.setFaculty(facultyUser2);
	    course3.setCode("CS779/879");
	    course3.setName("Design of Network Protocols");
	    course3.addStudent(studentUser1);
	    course3 = courseRepository.saveOrUpdate(course3);
	    
	    Course course4 = new Course();
	    course4.setCrn(75896L);
	    course4.setFaculty(facultyUser2);
	    course4.setCode("CS476/576");
	    course4.setName("Systems Programming");
	    course4.addStudent(studentUser2);
	    course4 = courseRepository.saveOrUpdate(course4);
	    
	    studentUser1.addRegisteredCourse(course1);
	    studentUser1.addRegisteredCourse(course3);
	    studentUser1.addRegisteredCourse(course4);
	    studentUser1 = userRepository.saveOrUpdate(studentUser1);	

	    studentUser2.addRegisteredCourse(course1);
	    studentUser2.addRegisteredCourse(course2);
	    studentUser2.addRegisteredCourse(course4);
	    studentUser2 = userRepository.saveOrUpdate(studentUser2);
	    
	    studentUser3.addRegisteredCourse(course2);
	    studentUser3.addRegisteredCourse(course3);
	    studentUser3 = userRepository.saveOrUpdate(studentUser3);
	    
	    studentUser4.addRegisteredCourse(course2);
	    studentUser4.addRegisteredCourse(course3);
	    studentUser4 = userRepository.saveOrUpdate(studentUser4);	    
	    
	    // Documents
	    
	    Document doc1 = docRepository.saveOrUpdate(new Document("CS772 Syllabus.doc",facultyUser1));
	    doc1.setFileContent("This is my file content");
	    facultyUser1.addDocument(doc1);
	    userRepository.saveOrUpdate(facultyUser1);
	    course1.addDocument(doc1);
	    courseRepository.saveOrUpdate(course1);
	    studentUser2.addDocument(doc1);
	    userRepository.saveOrUpdate(studentUser2);

	    Document doc2 = docRepository.saveOrUpdate(new Document("IPSec.ppt",facultyUser1));
	    facultyUser1.addDocument(doc2);
	    userRepository.saveOrUpdate(facultyUser1);
	    course1.addDocument(doc2);
	    courseRepository.saveOrUpdate(course1);
	    	    
	    Document doc3 = docRepository.saveOrUpdate(new Document("CS471 Syllabus",facultyUser1));
	    facultyUser1.addDocument(doc3);
	    userRepository.saveOrUpdate(facultyUser1);
	    course2.addDocument(doc3);
	    courseRepository.saveOrUpdate(course2);
	    
	    Document doc4 = docRepository.saveOrUpdate(new Document("Threading.ppt",facultyUser1));
	    facultyUser1.addDocument(doc4);
	    userRepository.saveOrUpdate(facultyUser1);
	    course2.addDocument(doc4);
	    courseRepository.saveOrUpdate(course2);
	    
	    Document doc5 = docRepository.saveOrUpdate(new Document("IPC.ppt",facultyUser1));
	    facultyUser1.addDocument(doc5);
	    userRepository.saveOrUpdate(facultyUser1);
	    course2.addDocument(doc5);
	    courseRepository.saveOrUpdate(course2);
	    studentUser1.addDocument(doc5);
	    userRepository.saveOrUpdate(studentUser1);
	    
	    Document doc6 = docRepository.saveOrUpdate(new Document("Test 1 Results.docx",facultyUser2));
	    facultyUser2.addDocument(doc6);
	    userRepository.saveOrUpdate(facultyUser2);
	    course2.addDocument(doc6);
	    courseRepository.saveOrUpdate(course2);
	    facultyUser1.addDocument(doc6);
	    userRepository.saveOrUpdate(facultyUser1);
	    studentUser1.addDocument(doc6);
	    userRepository.saveOrUpdate(studentUser1);
		
	    completed=true;
	}
	
}
